package com.tianyang.yukuaifuzs.business.main.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.main.bean.DeviceTypeOutput;
import com.tianyang.yukuaifuzs.business.main.contract.DeviceListContract;
import com.tianyang.yukuaifuzs.business.main.contract.MainConfig;
import com.tianyang.yukuaifuzs.business.main.presenter.DeviceListPresenter;
import com.tianyang.yukuaifuzs.business.main.provider.MainOpenProvider;
import com.tianyang.yukuaifuzs.jump.eventbus.RefreshDeviceEvent;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;

/**
 * Description：设备列表
 * Created by xyj on 2024/4/19
 */
public class DeviceListActivity extends BaseTitleActivity implements DeviceListContract.View {

    @BindView(R.id.deviceNumTv)
    TextView deviceNumTv;
    @BindView(R.id.containerLl)
    LinearLayout containerLl;

    private DeviceListContract.Presenter mPresenter;

    private String userId = null;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_device_list;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("设备列表");
        if (getIntent().hasExtra(MainConfig.INTENT_USER_ID)) {
            userId = getIntent().getStringExtra(MainConfig.INTENT_USER_ID);
        }

        mPresenter = new DeviceListPresenter(this, userId);
        mPresenter.getDeviceTypeList();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showDeviceTypeList(JSONObject jsonObject) {
        if (jsonObject != null) {
            if (jsonObject.containsKey("totalNumber")) {
                deviceNumTv.setText(jsonObject.getDouble("totalNumber").intValue() + "");
            }
            if (jsonObject.containsKey("list")) {
                List<DeviceTypeOutput> outputs = JSON.parseObject(jsonObject.getString("list"),
                        new TypeReference<List<DeviceTypeOutput>>() {
                        });
                if (outputs != null && !outputs.isEmpty()) {
                    containerLl.removeAllViews();
                    for (DeviceTypeOutput output : outputs) {
                        if (output != null) {
                            containerLl.addView(onCreateDeviceTypeView(output));
                        }
                    }
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private View onCreateDeviceTypeView(@NonNull DeviceTypeOutput output) {
        View view = View.inflate(mContext, R.layout.item_device_type, null);
        SimpleDraweeView deviceTypeSdv = view.findViewById(R.id.deviceTypeSdv);
        TextView deviceNameTv = view.findViewById(R.id.deviceNameTv);
        TextView deviceNumTv = view.findViewById(R.id.deviceNumTv);
        if (!TextUtils.isEmpty(output.getImageUrl()))
            deviceTypeSdv.setImageURI(output.getImageUrl());
        deviceNameTv.setText(VerifyUtil.checkString(output.getName()));
        deviceNumTv.setText("数量：" + output.getNumber());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainOpenProvider.getInstance().openCodePlateListActivity(mContext, output.getType(),
                        output.getName(), userId);
            }
        });
        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshDeviceEvent(RefreshDeviceEvent event) {
        if (event != null && event.isAddDevice()) {
            mPresenter.getDeviceTypeList();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(DeviceListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
